package MyBlog.action;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import MyBlog.biz.AlbumBiz;
import MyBlog.biz.PhotoBiz;
import MyBlog.biz.UserBiz;
import MyBlog.entity.Album;
import MyBlog.entity.Photo;
import MyBlog.entity.User;

import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Component("albumAction")
public class AlbumAction extends ActionSupport {

	public int getUserId() {
		return userId;
	}
	public int getAlbumId() {
		return albumId;
	}
	public String getMsg() {
		return msg;
	}
	public void setCover(File cover) {
		this.cover = cover;
	}
	public void setCoverFileName(String coverFileName) {
		this.coverFileName = coverFileName;
	}
	public void setAlbum(Album album) {
		this.album = album;
	}
	public Album getAlbum() {
		return album;
	}

	// 数组
	private List<Album> albumList;
	private List<Photo> photos;	
	public List<Photo> getPhotos() {
		return photos;
	}
	public List<Album> getAlbumList() {
		return albumList;
	}

	// 参数
	private int userId;
	private User user;
	private int albumId;
	private Album album;
	private String msg;
	public void setAlbumId(int albumId) {
		this.albumId = albumId;
	}
	public User getUser() {
		return user;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}

	// 实现类
	@Autowired
	private AlbumBiz albumBiz;
	@Autowired
	private UserBiz userBiz;
	@Autowired
	private PhotoBiz photoBiz;

	// 方法
	public String list() {
		user = userBiz.fetchUserById(userId);
		album = albumBiz.getAlbumById(albumId);
		photos = photoBiz.getPhotosByUserIdAndAlbumId(albumId, user.getId());
		albumList = albumBiz.getAlbumsById(userId);
		return SUCCESS;
	}
	
	public String albumManage(){
		user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		albumList = albumBiz.getAlbumsById(user.getId());
		return SUCCESS;
	}
	
	private File cover;
	private String coverFileName;
	
	public String add() throws IOException{
		user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		album.setUser(user);
		albumBiz.add(album);
		String vpath = "";
		if(cover != null){
			String exc = coverFileName.substring(coverFileName.lastIndexOf("."));
			vpath = 1+exc;
			String realPath = ServletActionContext.getServletContext().getRealPath(
					"UserFiles/Albums/"+user.getId()+"/"+album.getId()+"/Thumbnail"+"/"+vpath);
			File file = new File(realPath);
			realPath = "C:/程序设计/作业/it_homework/MyBlog/src/main/webapp"+"/UserFiles/Albums/"+album.getUser().getId()+"/"+album.getId()+"/Thumbnail/"+vpath;
			System.out.println(realPath);
			File destFile = new File(realPath);
			FileUtils.copyFile(cover, destFile);
			FileUtils.copyFile(cover,file);
		}
		album.setCover(vpath);
		albumBiz.update(album);
		return SUCCESS;
	}
	
}
